package com.iyue.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 短信邀请地址跳转
 * 
 * @author luozm
 */
public class InviteServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6827832951453484854L;

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            http://wap.10658002.com/m/invite/5_100000/0 the request send
	 *            by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String target = request.getRequestURI();
		System.out.println(target);
		target = target.substring(8);
		String[] params = target.split("/");
		if (params != null && params.length > 0) {
			StringBuilder realUrl = new StringBuilder();
			realUrl
					.append("http://wap.10658002.com/m/friend.do?do=Invite.sms&f=");
			realUrl.append(params[0]);
			realUrl.append("&tt=");
			realUrl.append(params[1]);
			response.sendRedirect(realUrl.toString());
			return;
		} else {
			PrintWriter out = response.getWriter();
			out.println("URL[" + target + "] invalid.");
			out.close();
		}
		if (target.matches("/")) {
			return;
		} else {

		}
	}

}
